package org.learn.a1_100_leet_code.util;

import org.learn.a1_100_leet_code.a_2.ListNode;

public class ConvertListNodeToArrayUtil {

    public static ListNode toListNode(int[] arr) {
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        for (int num : arr) {
            current.nextNode = new ListNode(num);
            current = current.nextNode;
        }
        return dummy.nextNode;
    }

    public static int[] toArray(ListNode head) {
        java.util.List<Integer> list = new java.util.ArrayList<>();
        while (head != null) {
            list.add(head.value);
            head = head.nextNode;
        }
        return list.stream().mapToInt(i -> i).toArray();
    }
}
